package site.edody.dframe.websocket;

import org.java_websocket.WebSocket;
import org.java_websocket.handshake.ClientHandshake;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * DFrame 默认 WebSocket 监听器
 * @author Dody
 */
@Component
public class DFrameDefaultWebSocketListener implements DFrameWebSocketListener {

    private static final Logger log = LoggerFactory.getLogger(DFrameDefaultWebSocketListener.class);

    @Override
    public void onOpen(String serverName, WebSocket conn, ClientHandshake handshake) {
        log.info("[DFrame-{}] Default listener - Client connected", serverName);
        conn.send("Welcome to DFrame WebSocket Server!");
    }

    @Override
    public void onClose(String serverName, WebSocket conn, int code, String reason, boolean remote) {
        log.info("[DFrame-{}] Default listener - Client disconnected: {}", serverName, reason);
    }

    @Override
    public void onMessage(String serverName, WebSocket conn, String message) {
        log.info("[DFrame-{}] Default listener - Received message: {}", serverName, message);
        // 默认回声处理
        conn.send("DFrame Echo: " + message);
    }

    @Override
    public void onError(String serverName, WebSocket conn, Exception ex) {
        log.error("[DFrame-{}] Default listener - WebSocket error", serverName, ex);
    }
}